package com.sgb.kjwl.view.ui.activity.self.logistics;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityCargoPublishContractBinding;
import com.sgb.kjwl.di.transport.DaggerTransportComponent;
import com.sgb.kjwl.di.transport.TransportModule;
import com.sgb.kjwl.model.entity.transport.CargoPublishEntity;
import com.sgb.kjwl.view.constant.RouterKey;
import com.sgb.kjwl.viewmodel.transport.CargoPublishContractViewModel;
import com.swgk.core.base.di.AppComponent;

import javax.inject.Inject;

/**
 * @author: 杨畔
 * @date: 2/26/21 5:49 PM
 * @Description: 货源发布填写违约责任页面
 */
public class CargoPublishContractActivity extends AppActivity {

    @Inject
    CargoPublishContractViewModel viewModel;
    ActivityCargoPublishContractBinding binding;
    //数据实体类
    private CargoPublishEntity mEntity;

    public static void start(Activity activity, CargoPublishEntity entity, int requestCode) {
        Intent intent = new Intent(activity, CargoPublishContractActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra(RouterKey.DATA, entity);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerTransportComponent.builder()
                .appComponent(appComponent)
                .transportModule(new TransportModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void init() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_cargo_publish_contract);
        binding.setLifecycleOwner(this);
        binding.includeTitle.titleView.setText("违约责任");
        initEvent();
        initData();
    }

    private void initEvent() {
        binding.setOnClick(this);
        binding.includeTitle.ivBack.setOnClickListener(this);
    }

    private void initData() {
        mEntity = getIntent().getParcelableExtra(RouterKey.DATA);
        if (mEntity == null) {
            mEntity = new CargoPublishEntity();
        } else {
            syncData();
        }
    }

    private void syncData() {
        if (!TextUtils.isEmpty(mEntity.getLostPromise())) {
            binding.etReleaseCargoLost.setText(mEntity.getLostPromise());
        }
        if (!TextUtils.isEmpty(mEntity.getOverduePromise())) {
            binding.etReleaseCargoOverdue.setText(mEntity.getOverduePromise());
        }
        if (!TextUtils.isEmpty(mEntity.getOtherPromise())) {
            binding.etReleaseCargoOther.setText(mEntity.getOtherPromise());
        }
    }

    @Override
    public void onClick(View v) {
        if (v == binding.includeTitle.ivBack) {
            finish();
        } else if (v == binding.tvReleaseCargoSubmit) {
            checkData();
        }
    }

    /**
     * 检查数据并回传
     */
    private void checkData() {
        if (mEntity == null) return;
        if (binding.etReleaseCargoLost.getText() != null) {
            mEntity.setLostPromise(binding.etReleaseCargoLost.getText().toString());
        } else {
            mEntity.setLostPromise("");
        }
        if (binding.etReleaseCargoOverdue.getText() != null) {
            mEntity.setOverduePromise(binding.etReleaseCargoOverdue.getText().toString());
        } else {
            mEntity.setOverduePromise("");
        }
        if (binding.etReleaseCargoOther.getText() != null) {
            mEntity.setOtherPromise(binding.etReleaseCargoOther.getText().toString());
        } else {
            mEntity.setOtherPromise("");
        }
        Intent intent = new Intent();
        intent.putExtra(RouterKey.DATA, mEntity);
        setResult(RESULT_OK, intent);
        finish();
    }
}
